//
//  CoursesViewController.m
//  Grades
//
//  Created by David Weiss on 3/26/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CoursesViewController.h"
#import "CourseInfoViewController.h"


@implementation CoursesViewController

- (id)initWithStyle:(UITableViewStyle)style
{
   if (self = [super initWithStyle:style])
	{
		[self setTitle:@"Courses"];
	}
	return self;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {

}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   [super viewDidLoad];
   // add the edit button
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
   
	//Initialize the array
	listOfItems = [[NSMutableArray alloc] init];
	
   // setup the data
	NSMutableArray *coreClassArray = [NSMutableArray arrayWithObjects:@"CIT 320", @"CIT 370", @"CIT 455", nil];
	NSDictionary *coreClassDict = [NSDictionary dictionaryWithObject:coreClassArray forKey:@"Courses"];
	
	NSMutableArray *electiveClassArray = [NSMutableArray arrayWithObjects:@"MUS 105", @"REL 211", nil];
	NSDictionary *electiveClassDict = [NSDictionary dictionaryWithObject:electiveClassArray forKey:@"Courses"];
	
	//Add items
	[listOfItems addObject:coreClassDict];
	[listOfItems addObject:electiveClassDict];
}

// Prepare UITableView to display data
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [listOfItems count];
}

// Customize the number of rows in the table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
{
	// Number of rows it should expect, based on the section
	NSDictionary *dictionary = [listOfItems objectAtIndex:section];
	NSMutableArray *array = [dictionary objectForKey:@"Courses"];
	return [array count];
}

// Display the header text
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return @"Core Classes";
	else
		return @"Electives";
}

// Display data in a table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
		autorelease];
	}

	NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Courses"];
	NSString *cellValue = [array objectAtIndex:indexPath.row];
	cell.text = cellValue;
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	// get the selected class
	NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Courses"];
	NSString *selectedClass = [array objectAtIndex:indexPath.row];

	CourseInfoViewController *infoView = [[CourseInfoViewController alloc] initWithStyle:UITableViewStyleGrouped];
	infoView.navigationItem.title = selectedClass;
	[self.navigationController pushViewController:infoView animated:YES];
	[infoView release];
	infoView = nil;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellAccessoryDetailDisclosureButton;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView beginUpdates];
	if (editingStyle == UITableViewCellEditingStyleDelete)
   {
		NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
		NSMutableArray *array = [dictionary objectForKey:@"Courses"];
		[array removeObjectAtIndex:indexPath.row];
		[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
	}
	[tableView endUpdates];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc
{
   [listOfItems release];
	[super dealloc];
}


@end
